﻿#include "TuxSpyNewSiteDlg.h"

wxString wlsversion_list[]= {
		wxT("WebLogic V8"),
		wxT("WebLogic V9"),
		wxT("WebLogic V10"),
		wxT("")
};

TuxSpyNewSiteDlg::TuxSpyNewSiteDlg( wxWindow* parent )
:
NewSiteDlg( parent )
{
}

void TuxSpyNewSiteDlg::OnInitDlg( wxInitDialogEvent& event )
{
	m_textSiteName->WriteText( m_site.SiteName );
	m_textSiteIP->WriteText( m_site.SiteIP );
	m_textSitePort->WriteText( m_site.SitePort );
	if( m_site.SiteName.IsEmpty() ) {
		m_flag = NEWSITE;
	} else {
		m_flag = MODIFYSITE;
	}
	m_textDatabaseFile->WriteText( m_site.DatabaseFile );

	if( m_site.SiteType == wxT("TUXEDO") ) {
		m_textSiteWlsUser->Disable();
		m_textAdminURL->Disable();
		m_choiceProducts->Select(0);
		m_choiceWlsVersion->Disable();
		m_textSiteWlsPwd->Disable();
	} else if( m_site.SiteType == wxT("WEBLOGIC") ){
		m_choiceProducts->Select(1);
		m_textSiteWlsUser->WriteText( m_site.SiteAuth );
		m_textAdminURL->WriteText( m_site.WlsAdminURL );
		m_textSiteWlsPwd->WriteText( m_site.SitePwd );

		for ( int i = 0; !wlsversion_list[i].IsEmpty(); i ++ ) {
			if( m_site.WlsVer == wlsversion_list[i] ) {
				m_choiceWlsVersion->Select(i);
				break;
			}
		};
	} else { //Add a new Site 
		m_choiceProducts->Select(0);
		m_textSiteWlsUser->Disable();
		m_textAdminURL->Disable();
		m_choiceWlsVersion->Disable();
		m_textSiteWlsPwd->Disable();
	}

	if( ! m_site.SiteName.empty() )	 { //Modify Now
		m_textSiteName->Disable();
		m_choiceProducts->Disable();
	}

}

void TuxSpyNewSiteDlg::SetSite( Site site)
{
	m_site = site;
}


Site TuxSpyNewSiteDlg::GetSite( )
{
	return m_site;
}

void TuxSpyNewSiteDlg::OnProductChoice( wxCommandEvent& event )
{
	int nSelect = event.GetSelection();
	switch( nSelect ) {
		case 0: //Tuxedo
			m_textSiteWlsUser->Clear();
			m_textSiteWlsUser->Disable();
			m_textAdminURL->Clear();
			m_textAdminURL->Disable();
			m_choiceWlsVersion->Disable();
			m_textSiteWlsPwd->Disable();
			break;
		case 1: //WebLogic
			m_textSiteWlsUser->Enable();
			m_textAdminURL->Enable();
			m_choiceWlsVersion->Enable();
			m_textSiteWlsPwd->Enable();
			break;
		default:
			break;
	}

}

void TuxSpyNewSiteDlg::OnOKButton( wxCommandEvent& event )
{
	wxChar SiteType[][12] = { wxT("TUXEDO"), wxT("WEBLOGIC") };

	if ( m_textSiteName->GetValue().IsEmpty() ) {
		wxMessageBox(wxT("Must Input Name!!"));
		return;
	}
	if ( m_textSiteIP->GetValue().IsEmpty() || m_textSitePort->GetValue().IsEmpty() ) {
		wxMessageBox(wxT("IP and PORT not set!!"));
		return;
	}
	if( m_choiceProducts->GetSelection() == 1 ) {// WebLogic
		if ( m_textSiteWlsUser->GetValue().IsEmpty() || m_textAdminURL->GetValue().IsEmpty() ) {
			wxMessageBox(wxT("WebLogic User/Pwd  or Admin URL not set!!"));
			return;
		}
	}

	m_site.SiteName = m_textSiteName->GetValue();
	m_site.SiteIP = m_textSiteIP->GetValue();
	m_site.SitePort = m_textSitePort->GetValue();
	m_site.SiteType = SiteType[m_choiceProducts->GetSelection()];
	m_site.SiteAuth = m_textSiteWlsUser->GetValue();
	m_site.WlsAdminURL = m_textAdminURL->GetValue();
	m_site.SitePwd = m_textSiteWlsPwd->GetValue();
	if( m_choiceProducts->GetSelection() == 1 ) {// WebLogic
		m_site.WlsVer = wlsversion_list[m_choiceWlsVersion->GetSelection()];
	}

	if( m_flag == NEWSITE ) {
		//wxString StdDataDir = wxStandardPaths::Get().GetDataDir();
		//wxString dbname = StdDataDir + wxT("\\") + m_site.SiteName + wxT(".db"); 	
		wxString dbname = m_site.SiteName + wxT(".db"); 	
		m_site.DatabaseFile = dbname;
	} else {
		m_site.DatabaseFile =  m_textDatabaseFile->GetValue();
	}

	SetReturnCode( IDOK );
	Destroy();

	return;
}